package com.richman.controller;

import com.richman.hg.HGGrabThread;
import com.richman.hgps38.HGISNMergeThread;
import com.richman.hgps38.ISNPS38EventManager;
import com.richman.hgps38.HGPS38MergeThread;
import com.richman.hgps38.ISNHGServerThread;
import com.richman.hgps38.ISNServerThread;
import com.richman.hgps38.ServerThread;
import com.richman.isn.ISNGrabThread;
import com.richman.ps38.PS38GrabThread;
import com.richman.ps38.PS38GranDanshiThread;
import com.richman.ps38.PS38Manager;
import com.richman.view.MainWindow;
import com.richman.view.SettingPane;

import java.awt.BorderLayout;

import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;


public class DecreseRatio {
	
	public static boolean bStartEngine = false;
	
	public static long startEngineTime = 0;
	
	public static PS38GrabThread ps38GrabThread = null;
	public static PS38GranDanshiThread ps38GranDanshiThread = null;
	
	static HGGrabThread hgGrabThread = null;
	static ISNGrabThread isnGrabThread = null;
	
	static HGPS38MergeThread hgps38MergeThread = null;
	
	static HGISNMergeThread hgisnMergeThread = null;
	
	static ServerThread serverThread = null;
	
	static ISNServerThread isnServerThread = null;
	
	static ISNHGServerThread isnhgServerThread = null;
	
	public static JButton btnpData;
	
	public static Button btnConnect;
	
	public  boolean blogTofile = true; 
	
	
	public static Button btnPMergeWnd;
	
	public static Button btnbetlist;
	
	public static Button btnNewPMergeWnd;
	
	public static Button btnPP8Wnd;
	
	public static Button btnPHGWnd;
	
	public static Button btnPZhiboWnd;
	
	public static Button btnZhiboConnect;
	
	public static Button btnMergeWnd;
	
	public static Button btnNewMergeWnd;
	
	public static Button btnChangepw;
	
	public static boolean connected = false;
	
	public static boolean zhiboConnected = false;
	
	
	
	public static boolean showMergeWnd = false;
	public static boolean showP8 = false;
	
	public static boolean showhg = false;
	public static boolean showZhibo = false;
	

	

	
	public static DecreseRatio sa;
	

	
	public TextField textFieldProxyAddress;
	
	public TextField textFieldProxyAccount;
	
	public TextField textFieldZhiboProxyAddress;
	
	public TextField textFieldZhiboProxyAccount;
	
	
	public TextField textFieldhgaddress;
	public TextField textFieldhgaccount;
	public TextField textFieldhgpwd;
	public  Button btnlogintohg;
	
	public static boolean blogintohg = false;
	
	public PrintStream psFile = null;
	public PrintStream psConsole = null;
	
	
	
	

	

	public static String address = "";
	public static String account = "";
	public static String password = "";
	
	
	public static MainWindow mainWnd = new MainWindow();

	
	
	
	
	public static ISNPS38EventManager isnps38EventManager = new ISNPS38EventManager();
	
	
	//public static SettingPane settingPane = new SettingPane();

	
	//public static boolean blogintohg = false;
	
	//public static void read
	
	public static void main(String[] args){
		
		System.setProperty("webdriver.chrome.driver","C:\\chromedriver_win32\\chromedriver.exe");
		

		readloginparams();

		loginFrame();
	}
	
	public static void readloginparams(){
		try{
			
			File file = new File("account/" + "hgloginparams.txt");
			
			if(!file.exists()){
				return;
			}

			BufferedReader freader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			
			String str = "";

			while ((str = freader.readLine()) != null) {
				
				if(str.contains("address")){
					address = str.replace("address:", "");
				}else if(str.contains("account")){
					account = str.replace("account:", "");
				}else if(str.contains("password")){
					password = str.replace("password:", "");
				}

				}
			
			
			
			
			if(null != freader){
				freader.close();
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void saveloginparams(){
		try{
			
			File file = new File("account/" + "hgloginparams.txt");

			if(!file.exists()){

				
				file.createNewFile();
			}
			
			BufferedWriter fwlocal = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));

			String str = "address:" + address;
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			
			str = "account:" + account;
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			
			str = "password:" + password;
			fwlocal.append(str);
			fwlocal.newLine();
			fwlocal.flush();
			
			fwlocal.close();
			
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	
	
	
	public static void loginFrame(){

		try{
			
			final JFrame lFrame = new JFrame("登陆");
			
			
			final Container container = lFrame.getContentPane();
			
			container.setLayout(new BorderLayout());

			//lFrame.setLayout(null);

			JPanel p = new JPanel(new GridLayout(3,2));
			
			container.add(p, BorderLayout.NORTH);
			
			
			JLabel pwpanel = new JLabel();
			pwpanel.setText("密码");
			pwpanel.setSize(50, 20);
			
			final JPasswordField pwtxt = new JPasswordField(40);
			
			final JButton btnConfirm = new JButton("确认");
			
			
			pwtxt.addKeyListener(new KeyAdapter(){ 
			      public void keyPressed(KeyEvent e)    
			      {    
			        if(e.getKeyChar()==KeyEvent.VK_ENTER )   //按回车键执行相应操作; 
			        { 
			        	btnConfirm.doClick();
			        } 
			      } 
			    });
			
			pwtxt.setEchoChar('*');
			
			
			BufferedWriter fw = null;  //写	
			BufferedReader reader = null; //读
			
			String pwinfile = null;
			
			
			File dir = new File("data");
			if (dir.exists()) {
			} else {
				dir.mkdirs();
			}
			
			File file = new File("data/" + "pw"
					+ ".conf");
			
			
			if(!file.exists()){
				fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"));
				fw.close();
			}else{
				reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); // 指定读取文件的编码格式，要和写入的格式一致，以免出现中文乱码,
				
				String str = null;
				
				String lastLine = null;
				
				while ((str = reader.readLine()) != null) {

						pwinfile = str;
						
					}
				
			
			}
			
			final String fpw = pwinfile;
			
			

			
			
			
			
			btnConfirm.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					String pw = pwtxt.getText();
					
					if(fpw != null && pw.equals(fpw)){
						

						sa = new DecreseRatio();
						
						sa.launchFrame();
						
						lFrame.setVisible(false);
					}else{
						JOptionPane.showMessageDialog(null, "请输入正确的密码,如忘记密码，请联系管理员", "密码错误", JOptionPane.ERROR_MESSAGE);
					}
					
					

					}
				});

			
			p.add(pwpanel);
			p.add(pwtxt);
			p.add(btnConfirm);
			
			lFrame.setLocation(400, 400);
			
			lFrame.setSize(250, 150);

			lFrame.setVisible(true);
			
			lFrame.setResizable(false);
			
			lFrame.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					System.exit(0);

				}
			});
			
			
		
//			serverThread = new ServerThread();
//			
//			serverThread.start();
			
			isnServerThread = new ISNServerThread();
			
			isnServerThread.start();
			
//			isnhgServerThread = new ISNHGServerThread();
//			isnhgServerThread.start();

			sa = new DecreseRatio();
			
			sa.launchFrame();
			
			lFrame.setVisible(false);
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		

		
		
		
	}
	
	
	
	public void launchFrame() {
		
		
		if(blogTofile == true) {
			try {
				// 生成路径
				File dir = new File("log");
				if (dir.exists()) {
				} else {
					dir.mkdirs();
				}

				// 把输出重定向到文件
				
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");// 设置日期格式
				
				psFile = new PrintStream("log/" + df.format(new Date())
							+ ".txt");
				
				
				psConsole = System.out;

				System.setOut(psFile);
				System.setErr(psFile);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		
		


		mainWnd.setVisible(true);
		
		mainWnd.generateForbidLeagueItem();
		
	}

	
	
	
	public static void StartEngine() {
		
		if(isnGrabThread != null && !isnGrabThread.getbexit()) {
			JOptionPane.showMessageDialog(null, "停止未完成，请稍后重启", "提示", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		
		if(hgGrabThread != null && !hgGrabThread.getbexit()) {
			JOptionPane.showMessageDialog(null, "停止为完成，请稍后重启", "提示", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		
		if(ps38GrabThread != null && !ps38GrabThread.getbexit()) {
			JOptionPane.showMessageDialog(null, "停止为完成，请稍后重启", "提示", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		
		

		bStartEngine = true;
		
		
		startEngineTime = System.currentTimeMillis();
		
		
//		isnGrabThread = new ISNGrabThread();
//		isnGrabThread.start();
		
//		hgGrabThread = new HGGrabThread();
//		
//		hgGrabThread.start();
				
		
		PS38Manager ps38Manager = new PS38Manager();
		ps38Manager.init();
		
		
		ps38GrabThread = new PS38GrabThread(ps38Manager);
		
		ps38GrabThread.start();
//		
//		ps38GranDanshiThread = new PS38GranDanshiThread(ps38Manager);
//		ps38GranDanshiThread.start();

		
//		hgps38MergeThread = new HGPS38MergeThread();
//		
//		hgps38MergeThread.start();
		
		//hgisnMergeThread = new HGISNMergeThread();
		//hgisnMergeThread.start();
		
	}

}
